$(function () {
    var gdId = getQueryString("originRecordId");
    $("#jqGrid").Grid({
        url: '../exceprecord/list?originRecordId='+gdId,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '状态', name: 'status', index: 'STATUS', width: 80, formatter: function (v) {
                    return sysEnums.EXCEPT_STATUS[v]
                }
            },
            {label: '异常现象', name: 'excepCon', index: 'EXCEP_CON', width: 80},
            {label: '问题描述', name: 'excepDescr', index: 'EXCEP_DESCR', width: 80},
            {label: '建议', name: 'advice', index: 'ADVICE', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {label: '责任人', name: 'dutyUserName', index: 'DUTY_USER_NAME', width: 80},
            {
                label: '关闭时间', name: 'colseDate', index: 'COLSE_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '添加时间', name: 'createDate', index: 'CREATE_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '消除确认', name: 'isCheck', index: 'IS_CHECK', width: 80, formatter: function (v) {
                    return sysEnums.CKECK_EXCEPT[v]
                }
            },

        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        showLib: false,
        title: null,
        exceptStatus: enumsToList('EXCEPT_STATUS'),
        excepRecord: {
            workshopName: '',
            isRepair: '',
            status: '',
            devId: '',
            devName: '',
            devCode: '',
            exType: '',
            id: '',
            remark: '',
            colseDate: '',
            dutyUserName: '',
            dutyUserId: '',
            advice: '',
            excepDescr: '',
            excepCon: '',
            workName: '',
            workId: '',
            workNo: '',
            proLineId: '',
            proLineName: '',
            workshopId: '',
            delFlag: '',
            updateUser: '',
            updateDate: '',
            createUser: '',
            createDate: '',
            inspectionRecords: '',
        },
        devMainBase: {
            checkSt: '',
            methodType: ''
        },
        ruleValidate: {

           /* exType: [
                {required: true, message: '异常来源不能为空', trigger: 'blur'}
            ]*/
        },
        q: {
            name: '',
            status:''
        },
        isrepairTypeList: enumsToList("ISREPAIR_TYPE"),//是否加入维护库
        errorReseList: enumsToList("ERROR_RESE"),//异常来源
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;

            vm.title = "新增";
            vm.excepRecord = {
                workshopName: '',
                isRepair: '',
                status: '',
                devId: '',
                devName: '',
                devCode: '',
                exType: '',
                id: '',
                remark: '',
                colseDate: '',
                dutyUserName: '',
                dutyUserId: '',
                advice: '',
                excepDescr: '',
                excepCon: '',
                workName: '',
                workId: '',
                workNo: '',
                proLineId: '',
                proLineName: '',
                workshopId: '',
                delFlag: '1',
                updateUser: '',
                updateDate: '',
                createUser: '',
                createDate: '',
                inspectionRecords: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.excepRecord.id == (null || '') ? "../exceprecord/save" : "../exceprecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.excepRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../exceprecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../exceprecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.excepRecord = r.excepRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'status':vm.q.status},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },      //显示设备列表-使用设备码
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        vm.excepRecord.devCode = res.devCode;
                        vm.excepRecord.devName = res.devName;
                        vm.excepRecord.devId = res.id;
                        vm.excepRecord.proLineId = res.devProductCode;//生产线代码
                        vm.excepRecord.proLineName = res.devProductName;//生产线代码
                        vm.excepRecord.workshopName = res.devFarm;//车间
                        layer.close(index);
                    }
                }
            })
        },
        goIsRepair: function (id, devId, isRepair) {
            //根据devId 查询
            if (isRepair == 0) {
                //未添加维护库
                var excDate;
                Ajax.request({
                    url: "../exceprecord/info/" + id,
                    async: false,
                    successCallback: function (r) {
                        excDate = r.excepRecord;
                    }
                });
                openWindow({
                    title: '添加维护库',
                    content: jQuery("#addLib"),
                    area: ['40%', '40%'],
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        vm.devMainBase.name = excDate.excepCon;
                        vm.devMainBase.methodType = excDate.advice;
                        vm.devMainBase.remark = excDate.remark;
                        //更新原数据表状态
                        Ajax.request({
                            url: '../exceprecord/addLib',
                            type: 'POST',
                            params: {"id": id},
                            async: false,
                            successCallback: function (r) {
                                alert("操作成功", function () {
                                    vm.reload();
                                });
                            }
                        });
                        //添加到维护库
                        Ajax.request({
                            url: "../devmainbase/save",
                            params: JSON.stringify(vm.devMainBase),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {

                                    vm.reload();
                                });
                            }
                        });
                        layer.close(index);
                    }
                });
            } else {
                alert("当前记录已添加维护库", function () {
                    vm.reload();
                })
            }

        },
        check: function () {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            var id = ids[0];
            var date = getSelectedRowData("#jqGrid");
            if (date.status == '异常已消除') {
                if (date.isCheck == '已确认') {
                    alert("当前已确认", function () {
                        vm.reload();
                    });
                } else {
                    Ajax.request({
                        url: '../exceprecord/updateCheck',
                        type: 'POST',
                        params: {"id": id},
                        async: false,
                        successCallback: function (r) {
                            alert("操作成功", function () {
                                vm.reload();
                            });
                        }
                    })
                }
            } else {
                alert("请消除异常", function () {
                    vm.reload();
                })
            }


        },
        clear: function () {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            var date = getSelectedRowData("#jqGrid");
            if (date.status == '异常已消除') {
                alert("异常已消除", function () {
                    vm.reload();
                })
            } else {
                Ajax.request({
                    url: '../exceprecord/updateStatus',
                    type: 'POST',
                    params: {"id": ids[0]},
                    async: false,
                    successCallback: function (r) {
                        alert("操作成功", function () {
                            vm.reload();
                        });
                    }
                });
            }
        }

    }
});
